#include "Widget.h"
#include "ui_Widget.h"
#include<QTimer>
#include<QKeyEvent>
#include<QPushButton>
#include<QToolButton>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setFixedSize(1255,805);

    mGameView.setSceneRect(QRect(0,0,1255,805));
    mScene.setSceneRect(QRect(0,0,1255,805));//游戏进行场景
    mStartScene.setSceneRect(QRect(0,0,1255,805));
    mStartScene.addPixmap(QPixmap(":/img/Image/07e5d72ef04c23c05e46d3fea77821a.jpg"));

    auto startBtn=new QToolButton();
    startBtn->setAutoRaise(true);
    startBtn->resize(155,45);
    startBtn->setIcon(QIcon(":/img/Image/953110f5c87cbf9f837b5f94097d864.png"));
    startBtn->setIconSize(QSize(155,45));
    startBtn->move(550,350);
    connect(startBtn,&QToolButton::clicked,[this](){
        mGameView.setScene(&mScene);
        mGameView.show();
    });


    mStartScene.addWidget(startBtn);

    mBackGround1.setPixmap(QPixmap(":/img/Image/07e5d72ef04c23c05e46d3fea77821a.jpg"));
    mBackGround2.setPixmap(QPixmap(":/img/Image/07e5d72ef04c23c05e46d3fea77821a.jpg"));
    mPlane.setPixmap(QPixmap(":/img/Image/f8b0338a99e351085829a83a7a41649.jpg"));

    mBackGround2.setPos(0,-mBackGround2.pixmap().height());

    //将图片元素添加至场景
    mScene.addItem(&mBackGround1);
    mScene.addItem(&mBackGround2);

    mScene.addItem(&mPlane);
    //设置视图场景
  //  mGameView.setScene(&mScene);
    mGameView.setScene(&mStartScene);
    mGameView.setParent(this);
    mGameView.show();

    //开启背景移动定时器
    mBGMoveTimer=new QTimer(this);
    mBGMoveTimer->start(10);
    connect(mBGMoveTimer,&QTimer::timeout,this,&Widget::BGMove);

    //开启飞机移动检测定时器
    mPlaneMoveTimer=new QTimer(this);
    mPlaneMoveTimer->start(10);
    connect(mPlaneMoveTimer,&QTimer::timeout,this,&Widget::PlaneMove);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::BGMove()
{
    mBackGround2.moveBy(0,7);
    mBackGround1.moveBy(0,7);//moveBy移动量
    if(mBackGround1.y()>=mBackGround1.pixmap().height())
    {
        mBackGround1.setY(-mBackGround1.pixmap().height());
    }
    else if(mBackGround2.y()>=mBackGround2.pixmap().height())
    {
        mBackGround2.setY(-mBackGround2.pixmap().height());
    }
}

void Widget::PlaneMove()
{
    for(int KeyCode : mKeyList)
    {
        switch (KeyCode) {
        case Qt::Key_W:
            mPlane.moveBy(0,-1*mPlane.mMoveSpeed);
            break;
        case Qt::Key_S:
            mPlane.moveBy(0,1*mPlane.mMoveSpeed);
            break;
        case Qt::Key_A:
            mPlane.moveBy(-1*mPlane.mMoveSpeed,0);
            break;
        case Qt::Key_D:
            mPlane.moveBy(1*mPlane.mMoveSpeed,0);
            break;
        default:
            break;
        }
    }

    //边界判断
    if(mPlane.x()<0)
    {
        mPlane.setX(0);
    }
    if(mPlane.y()<0)
    {
        mPlane.setY(0);
    }
    if(mPlane.x()>this->width()-mPlane.pixmap().width())
    {
        mPlane.setX(this->width()-mPlane.pixmap().width());
    }
    if(mPlane.y()>this->height()-mPlane.pixmap().height())
    {
        mPlane.setY(this->height()-mPlane.pixmap().height());
    }
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    //添加按键到按键组合
    switch (event->key()) {
    case Qt::Key_W:
    case Qt::Key_S:
    case Qt::Key_A:
    case Qt::Key_D:
        mKeyList.append(event->key());
        break;
    default:
        break;
    }

}

void Widget::keyReleaseEvent(QKeyEvent *event)
{
    //移除按键组合
    if(mKeyList.contains(event->key()))
    {
        mKeyList.removeOne(event->key());
    }
}
